package com.example.fw;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 13.04.13
 * Time: 23:10
 */
public class HelperBase {
    protected final ApplicationManager manager;
    protected final JFrameOperator mainFrame;

    public HelperBase(ApplicationManager manager) {
        this.manager = manager;
        mainFrame = manager.getApplication();
    }

    protected String waitMessageDialog(String title, int timeOut) {
        JDialogOperator dialogOperator = waitDialog(title, timeOut);
        if (dialogOperator != null) {
            String message = new JLabelOperator(dialogOperator).getText();
            dialogOperator.requestClose();
            return message;
        }
        return null;
    }

    protected JDialogOperator waitDialog(String title, int timeOut) {
        long start = System.currentTimeMillis();
        long currentTime = start;
        while (currentTime < start + timeOut) {
            JDialog jDialog = JDialogOperator.findJDialog(mainFrame.getOwner(), title, false, false);
            if (jDialog != null) {
                return new JDialogOperator(jDialog);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            currentTime = System.currentTimeMillis();
        }
        return null;
    }

    protected void pushButtonInDialog(String titleDialog, String buttonText) {
        JDialogOperator dialogOperator = waitDialog(titleDialog, 3000);
        new JButtonOperator(dialogOperator, buttonText).clickMouse();
    }
}
